package com.autoTest.common;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class commonDriver {
    private static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        }
        return driver;
    }
    public List<String> getFmtFiles(){
        SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        String filename = sim.format(System.currentTimeMillis());
        sim = new SimpleDateFormat("yyyy-MM-dd");
        String filesname = sim.format(System.currentTimeMillis());

        List<String> list = new ArrayList<>();
        list.add(filename);
        list.add(filesname);
        return list;
    }
    public  void getScreenShot(String str) throws IOException {
        File srcfile = driver.getScreenshotAs(OutputType.FILE);
        List<String> list = getFmtFiles();
        String filname = "./src/test/autopic"+list.get(1)+"/"+str+"-"+list.get(0)+".png";
        System.out.println(filname);
        File newfile = new File(filname);
        FileUtils.copyFile(srcfile,newfile);
    }
}
